export const LANG = 'lang'
import LangENUS from './en-us/index'
import LangZHCN from './zh-cn/index'
import { createI18n } from 'vue-i18n'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import en from 'element-plus/es/locale/lang/en'

type langType = 'zh-cn' | 'en-us'

export const getLang = function () {
  return sessionStorage.getItem(LANG) || 'zh-cn'
}
export const setLang = function (val: langType) {
  return sessionStorage.setItem(LANG, val)
}
export const getLangType = function () {
  return sessionStorage.getItem(LANG) == 'zh-cn' ? 1 : 2
}

const i18n = createI18n({
  locale: getLang() || 'zh-cn',
  messages: {
    'en-us': LangENUS,
    'zh-cn': LangZHCN
  }
})

// 设置element的语言
export const setElemantLocale = (app: any, val: string = i18n.global.locale) => {
  let locale = zhCn // 默认语言为中文
  if (val === 'en-us') {
    locale = en // 设置为英文
  }
  app.use(ElementPlus, {
    locale: locale
  })
}
export default i18n
